<template>
  <div class="blog-detail">
    <Spin v-if="loading" tip="加载中..." style="display: block; margin: 40px auto;">
      <div style="height: 400px;"></div>
    </Spin>
    
    <template v-else>
      <BlogHeader :blog="blog" />
      
      <div class="blog-content">
        <div class="github-markdown-body" v-html="blog.contentHtml"></div>
      </div>
      
      <div class="blog-tags">
        <Tag v-for="tag in blog.tags" :key="tag._id" color="blue">
          {{ tag.name }}
        </Tag>
      </div>
      
      <BlogNavigation :prevBlog="prevBlog" :nextBlog="nextBlog" />
    </template>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { Avatar, Button, Input, Tag, Spin } from 'ant-design-vue'
import { useBlogStore } from '../stores/blog'
import BlogHeader from '../components/BlogHeader.vue'
import BlogNavigation from '../components/BlogNavigation.vue'

const route = useRoute()
// 根据API文档，文章ID应该是字符串类型的_id，不需要转换为数字
const blogId = route.params.id

// 使用 Pinia store
const blogStore = useBlogStore()

// 博客文章数据
const blog = ref({
  _id: blogId,
  title: '',
  date: '',
  category: '',
  tags: [],
  content: '',
  summary: '',
  excerpt: '',
  contentHtml: '',
  views: 0,
  readTime: 0,
  coverImage: '',
  author: ''
})

// 前后篇导航
const prevBlog = ref(null)
const nextBlog = ref(null)

// 加载状态
const loading = ref(false)


// 获取博客文章详情
const fetchBlogDetail = async () => {
  try {
    loading.value = true
    const data = await blogStore.getArticleDetail(blogId)
    if (data) {
      blog.value = data
      // 获取前后篇文章信息
      await fetchPrevNextBlogs(blogId)
    }
  } catch (error) {
    console.error('获取博客详情失败:', error)
  } finally {
    loading.value = false
  }
}

// 获取前后篇文章
const fetchPrevNextBlogs = async (currentId) => {
  try {
    const data = await blogStore.getArticles({ page: 1, pageSize: 100 }) // 获取足够多的文章来找到前后篇
    if (data && data.list) {
      const articles = data.list
      const currentIndex = articles.findIndex(article => article._id === currentId)
      
      if (currentIndex > 0) {
        prevBlog.value = articles[currentIndex - 1]
      }
      
      if (currentIndex < articles.length - 1) {
        nextBlog.value = articles[currentIndex + 1]
      }
    }
  } catch (error) {
    console.error('获取前后篇文章失败:', error)
  }
}


// 组件挂载时获取数据
onMounted(() => {
  fetchBlogDetail()
})
</script>

<style scoped lang="less">
.blog-detail {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
}

.blog-content {
  background: white;
  border-radius: 12px;
  padding: 32px;
  margin-bottom: 32px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(0, 0, 0, 0.05);
  line-height: 1.8;
  color: #333;
  font-size: 1rem;
}

.blog-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin: 32px 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .blog-detail {
    padding: 20px 16px;
  }
  
  .blog-content {
    padding: 24px;
    margin-bottom: 24px;
  }
}
</style>